package com.fss.plant.websocket;


import com.alibaba.fastjson2.JSON;
import com.fss.framework.web.domain.server.Sys;
import com.fss.plant.domain.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.TextWebSocketHandler;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class MyWebSocketHandler extends TextWebSocketHandler {


    private static final  Map<String, WebSocketSession> sessions = new ConcurrentHashMap<>();
    @Override
    public void afterConnectionEstablished(WebSocketSession session) throws Exception {
        System.out.println("Client connected: " + session.getId());
        //System.out.println(sessions);
        //session.sendMessage(new TextMessage(session.getId()));
    }

    @Override
    protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
        String payload = message.getPayload();
        WebSocketSession webSocketSession=null;
        try{
            String mess = message.getPayload();
//            System.out.println(mess);
            List<Message> message1=JSON.parseArray(mess,Message.class);
            System.out.println(message1);
            if(message1 != null)
            {
//                if(sessions.get(message1.get(0).targetName)!=null)
//                {
                    System.out.println("当前发送消息的连接-----"+message1.get(0).targetName);
                    webSocketSession=sessions.get('"'+message1.get(0).targetName+'"');
                //}
                //System.out.println(message1.get(0).targetName);
               System.out.println(sessions);
                //System.out.println(webSocketSession);
                if(webSocketSession!=null){
                    for(Message m:message1)
                    {
                        String tmp = m.getBotContent();
                        m.setBotContent(m.userContent);
                        m.setUserContent(tmp);
                    }
                    System.out.println("向-----"+webSocketSession.toString()+"发送消息");
                    webSocketSession.sendMessage(new TextMessage(JSON.toJSONString(message1)));
                }
            }
//            System.out.println("------------------------------");
//            System.out.println(message1);
        }catch (Exception e){

            System.out.println(e);
            sessions.putIfAbsent(payload, session);
            System.out.println("当前加入连接-----"+payload);
        }
    }

    @Override
    public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {
//        for (Map.Entry<String, WebSocketSession> entry : sessions.entrySet())
//        {
//            if(entry.getValue().equals(session))
//            {
//                sessions.remove(entry.getKey());
//                break;
//            }
//        }
        sessions.values().removeIf(wsSession -> wsSession.equals(session));
        System.out.println("Client disconnected: " + session.getId());
        //System.out.println(sessions);
    }

    @Override
    public void handleTransportError(WebSocketSession session, Throwable exception) throws Exception {
        System.out.println("WebSocket error: " + exception.getMessage());
    }
}
